package com.finalist.controller.shop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.finalist.Page;
import com.finalist.Pageable;
import com.finalist.entity.Area;
import com.finalist.entity.Store;
import com.finalist.entity.Product.OrderType;
import com.finalist.service.AreaService;
import com.finalist.service.StoreService;
import com.sun.org.apache.xalan.internal.xsltc.compiler.sym;

@Controller("storeShopController")
@RequestMapping("/shop/store")
public class StoreController extends BaseController {

	@Resource(name = "storeServiceImpl")
	private StoreService storeService;
	@Resource(name = "areaServiceImpl")
	private AreaService areaService;
	
	@RequestMapping("/info")
	public String info(ModelMap model) {
		
		List<Area> areas = new ArrayList<Area>();
		areas = areaService.findRoots();
		
		Map<Long, List<Area>> areasList = new HashMap<Long, List<Area>>();
		for (Area area : areas) {
			
			 List<Area>  childrens = areaService.findChildrens(area.getId());
			 areasList.put(area.getId(), childrens);
		}
		model.addAttribute("areasList", areasList);
		return "/shop/store/info";
	}
	
	@RequestMapping("/getList/{areaId}")
	public String list(@PathVariable Long areaId,Pageable pageable,ModelMap model) {
		
		Area area = new Area();
		area = areaService.find(areaId);
		model.addAttribute("page", storeService.findPage(null, area,  OrderType.dateDesc, pageable));
		model.addAttribute("area", area);
		return "/shop/store/list";//门店管理数据添加完整以后 放开该注释
//		return "/shop/store/construction";
	}
	
	@RequestMapping("/search")
	public String search(Long areaId,Store store,Integer pageNumber, ModelMap model) {
		Area area = new Area();
		area = areaService.find(areaId);
		if (pageNumber == null)
        {
            pageNumber = 1;
        }
        Pageable pageable = new Pageable(pageNumber, 20);
//		model.addAttribute("page", storeService.findPage(store, area,  OrderType.dateDesc, null));
		model.addAttribute("page", storeService.findPage(store, area,  OrderType.dateDesc, pageable));
		model.addAttribute("areaId", areaId);
		model.addAttribute("area", area);
		model.addAttribute("store", store);
		return "/shop/store/list";//门店管理数据添加完整以后 放开该注释
		//return "/shop/store/construction";
	}

}
